<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/27
 * Time: 16:55
 */
//获取前端返回的数据
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$phonenum = $_REQUEST['phonenum'];
//连接数据库
$link=mysqli_connect('localhost','ni','123456')or die(json_encode(array('result'=>'connect database fail!')));
//选择要操作的库
mysqli_select_db($link,'ajaxdb')or die(json_encode(array('result'=>'select database error')));
//定义查询的sql语句
$sql_select="select username from ys3181msg where username = '$username'";
//执行sql，返回结果集
$res=mysqli_query($link,$sql_select);
//根据返回结果判断该用户名是否被注册
if ($username==''){
    echo json_encode(array('result'=>'username dose not allowed null'));
}else{
    if (mysqli_fetch_assoc($res)!=null){
        echo json_encode(array('result'=>'username is exist','username'=>$username));
    }else{
        $sql_insert="insert into ys3181msg(username,password,phonenum)values ('$username','$password','$phonenum')";
        mysqli_query($link,$sql_insert);
        if (mysqli_affected_rows($link)>0){
            echo json_encode(array('result'=>'success','username'=>$username));
        }else{
            echo json_encode(array('result'=>'.insert database fail'));
        }
    }
}

//    关闭数据库连接
mysqli_close($link);